﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace TrafficControl
{
    public partial class TrafficSimulationMainForm : Form
    {
        PictureBox car = new PictureBox();
        int y = 0;
        private Graphics g;
        Road road;
        TrafficSimulation currentTrafficSimulation;

        public void createNewTrafficSimulation() {
            this.currentTrafficSimulation = new TrafficSimulation();
        }

        public Boolean addNewCross(Position position) {
            return this.currentTrafficSimulation.addNewCross(position);
        }

        public TrafficSimulationMainForm()
        {
            InitializeComponent();
            road = new Road();
            Thread t = new Thread(road.Run);
            t.IsBackground = true;
            t.Start();
            //while (y < 666)
            //{
            //    y++;
            //    Thread.Sleep(100);
            //}
        }


        private void TrafficSimulation_Paint(object sender, PaintEventArgs e) {

            //BufferedGraphicsContext currentContext;
            //BufferedGraphics myBuffer;
            //// Gets a reference to the current BufferedGraphicsContext
            //currentContext = BufferedGraphicsManager.Current;
            //// Creates a BufferedGraphics instance associated with Form1, and with 
            //// dimensions the same size as the drawing surface of Form1.
            //myBuffer = currentContext.Allocate(this.CreateGraphics(), this.DisplayRectangle);
            //g = myBuffer.Graphics;

            //this.Invalidate();
            // Draw image to screen.
            drawCrosses();
            //drawTrafficLight();
            //myBuffer.Render(e.Graphics);
        
        }



        public void drawCrosses()
        {
            this.currentTrafficSimulation.getCrosses();
        }

        public Boolean saveCurrentTrafficSimulation(string filePath)
        {
            TrafficSimulationIO trafficSimulationIO = new TrafficSimulationIO();
            return trafficSimulationIO.saveSimulation(filePath, this.currentTrafficSimulation);
        }

        public TrafficSimulation loadTrafficSimulation(string filePath)
        {
            TrafficSimulationIO trafficSimulationIO = new TrafficSimulationIO();
            return trafficSimulationIO.loadSimulation(filePath);

        }

    }
}
